package com.hyk.commonLib.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.hyk.commonLib.R;
import com.hyk.commonLib.common.utils.ResUtils;
import com.hyk.commonLib.common.utils.ScreenUtils;

/**
 * Created by 贺玉琨 on 2019/11/27.
 */
public class CustomSizeDrawableButton extends GradientRoundCornerButton {
    public static final int SCALE_TYPE_FIT_XY = 0;
    public static final int SCALE_TYPE_FIT_WIDTH = 1;
    public static final int SCALE_TYPE_FIT_HEIGHT = 2;

    private int drawableSize;
    private int scaleType = SCALE_TYPE_FIT_XY;

    public CustomSizeDrawableButton(Context context) {
        this(context, null);
    }

    public CustomSizeDrawableButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomSizeDrawableButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomSizeDrawableButton);
        drawableSize = (int) mTypedArray.getDimension(R.styleable.CustomSizeDrawableButton_drawableSize, 0);
        scaleType = mTypedArray.getInt(R.styleable.CustomSizeDrawableButton_scaleType, SCALE_TYPE_FIT_XY);
        mTypedArray.recycle();

        Drawable[] compoundDrawables = getCompoundDrawables();
        setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
    }

    @Override
    public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        if (left != null) {
            resize(left);
        }
        if (top != null) {
            resize(top);
        }
        if (right != null) {
            resize(right);
        }
        if (bottom != null) {
            resize(bottom);
        }
        super.setCompoundDrawables(left, top, right, bottom);
    }

    private void resize(Drawable drawable) {
        float width = drawable.getIntrinsicWidth();
        float height = drawable.getIntrinsicHeight();
        if (scaleType == SCALE_TYPE_FIT_XY) {
            if (width == height) {
                drawable.setBounds(0, 0, drawableSize, drawableSize);
            } else if (width > height) {
                drawable.setBounds(0, 0, drawableSize, (int) (drawableSize * (height / width)));
            } else {
                drawable.setBounds(0, 0, (int) (drawableSize * (width / height)), drawableSize);
            }
        } else if (scaleType == SCALE_TYPE_FIT_WIDTH) {
            drawable.setBounds(0, 0, drawableSize, (int) (drawable.getIntrinsicHeight() / (float) drawable.getIntrinsicWidth() * drawableSize));
        } else if (scaleType == SCALE_TYPE_FIT_HEIGHT) {
            drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() / (float) drawable.getIntrinsicHeight() * drawableSize), drawableSize);
        }
    }

    public void setLeftDrawable(@Nullable Drawable drawable) {
        Drawable[] compoundDrawables = getCompoundDrawables();
        setCompoundDrawables(drawable, compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
    }

    public void setRightDrawable(@Nullable Drawable drawable) {
        Drawable[] compoundDrawables = getCompoundDrawables();
        setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], drawable, compoundDrawables[3]);
    }

    public void setTopDrawable(@Nullable Drawable drawable) {
        Drawable[] compoundDrawables = getCompoundDrawables();
        setCompoundDrawables(compoundDrawables[0], drawable, compoundDrawables[2], compoundDrawables[3]);
    }

    public void setBottomDrawable(@Nullable Drawable drawable) {
        Drawable[] compoundDrawables = getCompoundDrawables();
        setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], drawable);
    }

    public void setLeftDrawable(@DrawableRes int drawableRes) {
        setLeftDrawable(ResUtils.getDrawable(drawableRes));
    }

    public void setRightDrawable(@DrawableRes int drawableRes) {
        setRightDrawable(ResUtils.getDrawable(drawableRes));
    }

    public void setTopDrawable(@DrawableRes int drawableRes) {
        setTopDrawable(ResUtils.getDrawable(drawableRes));
    }

    public void setBottomDrawable(@DrawableRes int drawableRes) {
        setBottomDrawable(ResUtils.getDrawable(drawableRes));
    }

    public void clearDrawable() {
        setCompoundDrawables(null, null, null, null);
    }

    public void setDrawableSizeInDp(int drawableSize) {
        setDrawableSizeInPx(ScreenUtils.dp2px(drawableSize));
    }

    public void setDrawableSizeInPx(int drawableSize) {
        this.drawableSize = drawableSize;
        Drawable[] compoundDrawables = getCompoundDrawables();
        setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
    }
}
